import os
from gurobipy import *
import random
import math


class Data:
    timepiece = 0  # 实际代表时刻 从0时刻开始 相当于区间加1
    OrderNumber = 0 #假设订单0为depot，n+1为depot
    multipleNum = 1  # 对应转换关系 需要手动计算在txt文件中读取
    orderNum = []  # 订单编号
    customerNo = [] #对应的顾客编号
    orderTime = []  # 订单到来时间
    prepareTime = []  # 需要的拣货时间
    orderDemand = []  # 需求量
    serviceTime = []  # 服务时间
    dueTime = []    #最迟时间的软时间窗
    timeMatrix = [[]]  # 记录订单到达时间的区间 定义w(i_s)
    timeNum = []  # 每一时间切片前的订单数量 定义S(t_s)
#客户数据结构
class Customer:
    allNumber = 0   #包含配送中心
    allCus = []
    cor_X = []
    cor_Y = []
#车辆数据结构
class Vehicle:
    vehicleNum = 0
    vehicleCapacity = 0

def writecustomer(customer, path):
    f = open(path, 'r')
    lines = f.readlines()
    customer.allNumber = len(lines) - 9
    count = 0
    # print(len(lines))
    for line in lines:
        count = count + 1

        if count >= 10 and count < 10 + customer.allNumber:
            line = line[:-1]
            str = re.split(r" +", line)
            customer.allCus.append(int(count-10))
            customer.cor_X.append(float(str[2]))
            customer.cor_Y.append(float(str[3]))
    speed = 1
    return customer
def writeOrder(data,path,customer,orn,tp,mpn,ott):  #包含0和n+1
    customerSet = []
    for cus in range(1,customer.allNumber):
        customerSet.append(cus)
    cnt = 0
    data.OrderNumber = orn
    data.timepiece = tp
    data.multipleNum = mpn
    data.orderNum.clear()
    data.customerNo.clear()
    data.orderTime.clear()
    data.prepareTime.clear()
    data.orderDemand.clear()
    data.serviceTime.clear()
    data.dueTime.clear()
    while(cnt<10+orn-1):
        f = open(path, 'r')
        lines = f.readlines()
        for line in lines:
            cnt+=1
            if cnt ==10:
                data.orderNum.append(int(0))
                data.customerNo.append(int(0))
                data.orderTime.append(int(0))
                data.prepareTime.append(int(0))
                data.orderDemand.append(int(0))
                data.serviceTime.append(int(0))
                data.dueTime.append(int(1000))
            if cnt > 10 and cnt < 10 + orn - 1:
                line = line[:-1]
                str = re.split(r" +", line)
                data.orderNum.append(int(cnt-10))
                item = random.choice(customerSet)
                customerSet.remove(item)
                data.customerNo.append(int(item))
                data.orderTime.append(ott[cnt-11])
                data.prepareTime.append(int(5))
                data.orderDemand.append(float(str[4]))
                data.serviceTime.append(int(10))
                data.dueTime.append(ott[cnt-11]+300)
    data.orderNum.append(data.orderNum[0])
    data.customerNo.append(data.customerNo[0])
    data.orderTime.append(data.orderTime[0])
    data.prepareTime.append(data.prepareTime[0])
    data.orderDemand.append(data.orderDemand[0])
    data.serviceTime.append(data.serviceTime[0])
    data.dueTime.append(data.dueTime[0])
    return data

customer = Customer()
path = 'c101.txt'
writecustomer(customer, path)
# for i in range(customer.allNumber):
#     f = open('customer_.txt', 'a')
#
#     f.write("\n" + str(customer.allCus[i]) + "  " + str(customer.cor_X[i])
#             + "  " + str(customer.cor_Y[i]))
# if (os.path.exists('customer_.txt')):
#     f = open('customer_.txt', 'r+')
#     content = f.read()
#     f.seek(0, 0)
#     f.write("\n\nallnumber\n" + str(customer.allNumber)
#             + "\n\n\n\nallCus    cor_X    Cor_Y" + content)
sencenum = 40
path1 = './orderfile'
if not (os.path.exists(path1)):
    os.makedirs(path1)

for num in range(sencenum):
    data = Data()
    path = 'c101.txt'
    m = [5,15,15,5,20,15,5,10,5,5]
    mmerror = [1,3,3,1,4,3,1,2,1,1]
    mm = [0]*10
    error = 100
    eps1 = 10
    while(error>eps1):
        eps = 0
        for i in range(len(m)):
            mm[i] = random.randint(m[i]-mmerror[i],m[i]+mmerror[i])
            eps += abs(mm[i]-m[i])
        error = eps

    ott = []
    mnp = 30
    for i in range(len(mm)):
        for j in range(mm[i]):
            ott.append(math.floor(mnp*(i+random.random())))
    ott.sort()

    # for i in range(len(ott)):
    #     print(str(ott[i])+'  ')
    writeOrder(data,path,customer,orn=len(ott)+2,tp=10,mpn=30,ott=ott)

    #
    #
    #
    for i in range(data.OrderNumber):
        if(data.orderTime[i]+data.prepareTime[i] >= data.multipleNum*data.timepiece):
            data.orderTime[i] = data.orderTime[i]-data.prepareTime[i] - 1
            data.dueTime[i] = data.dueTime[i] -data.prepareTime[i] - 1
    for i in range(data.OrderNumber):
        if not (os.path.exists('../orderfile/order_'+str(num+1)+'.txt')):
            #os.mknod('../orderfile/order_'+str(num+1)+'.txt')
            f = open('../orderfile/order_' + str(num + 1) + '.txt', 'w+')
        f = open('orderfile/order_'+str(num+1)+'.txt', 'a')  #追加模式
        f.write("\n" + str(data.orderNum[i]) + "  " + str(data.customerNo[i])
                + "  " + str(data.orderTime[i]) + "  " + str(data.prepareTime[i]) + "  "
                + str(data.orderDemand[i])+ "  " + str(data.serviceTime[i]) + "  " + str(data.dueTime[i]) )
    if (os.path.exists('../orderfile/order_'+str(num+1)+'.txt')):
        f = open('orderfile/order_'+str(num+1)+'.txt', 'r+')
        content = f.read()
        f.seek(0, 0)
        f.write("\n\ntimepiece   multinum\n" + str(data.timepiece+1) + "    " + str(data.multipleNum)
                + "\n\n\n\noNum    cNo    oTime   pTime    oDemand  sTime    dueTime" + content)